<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Organization\Service;

use Plugin\Organization\Repository\RdOrganizeProjLineRepository;
use App\Service\IService;

/**
 * 项目线表服务类
 */
final class RdOrganizeProjLineService extends IService
{
    public function __construct(
        protected readonly RdOrganizeProjLineRepository $repository,
        protected readonly RdOrganizeAreaService $areaService,
    ) {}

    public function getAllowedUpdateData($data): array
    {
        return array_flip(['name', 'zone_id', 'sort', 'status', 'remark']);
    }

    /**
     * 根据大区ID删除项目线及其关联数据
     * @param mixed $id 大区ID或ID数组
     */
    public function deleteByZoneId(mixed $id): void
    {
        if (is_int($id)) {
            $id = [$id];
        }
        // 获取要删除的项目线ID
        $ids = $this->repository->getQuery()->whereIn('zone_id', $id)->pluck('id')->toArray();
        if (!empty($ids)) {
            // 先删除关联的工区（以及更深层的关联数据）
            $this->areaService->deleteByLineId($ids);
            // 再删除项目线本身
            $this->deleteById($ids);
        }
    }
}